//
//  ChooseMoreViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/11/2.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "ChooseMoreViewController.h"
#import "MsgStoreListTableViewCell.h"
#import "ShopAndOpenInforModel.h"//商户 或开办方
#import "CheckManListModel.h"//检查员
#import "SingleSearchView.h"
@interface ChooseMoreViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)TB_TableView *homeTableView;

@property(nonatomic,strong)NSMutableArray *dataArr;

@property(nonatomic,strong)UIButton*AddBtn;

@property(nonatomic,strong)ShopAndOpenInforModel* StoreModel;//商户

@property(nonatomic,strong)ShopAndOpenInforModel *OpenModel;//开办方

@property(nonatomic,strong)CheckManListModel *CheckModel;//检查员

@property(nonatomic,strong)CheckManListModel *ManagerModel;//管理员

@property(nonatomic,strong)UIButton*btn;

@property(nonatomic,strong)SingleSearchView *searchView;

@end

@implementation ChooseMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self.view addSubview:self.homeTableView];
    
    if (!self.isSingeChoose) {
        
         [self addEditBtn];
    }

    // Do any additional setup after loading the view.
}

-(void)addEditBtn
{
    self.btn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.btn.frame = CGRectMake(0, 0, 60, 20);
    
    [self.btn setTitle:@"全选" forState:UIControlStateNormal];
    
    [self.btn setTitle:@"取消" forState:UIControlStateSelected];
    
    self.btn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [self.btn sizeToFit];
    
    self.btn.tintColor=[UIColor clearColor];
    
    [self.btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:self.btn];
    
    self.navigationItem.rightBarButtonItem=item;
    
    [self.btn addTarget:self action:@selector(GotoEdit:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(NSMutableArray*)dataArr
{
    if (!_dataArr) {
        
        _dataArr=[NSMutableArray array];
        
    }
    
    return _dataArr;
}


-(SingleSearchView*)searchView
{
    if (!_searchView) {
        
        TBWeak;
        _searchView=[[SingleSearchView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 56) andSearchPloahe:@"输入关键字" andBlock:^(NSString *Key) {
            
            NSDictionary *dict=@{@"keyword":Key};
            
            weakSelf.Reqmodel.params=[HelpWay dictionaryToJSON:dict];
            
            [weakSelf.homeTableView.mj_header beginRefreshing];
            
        }];
        
    }
    return _searchView;
}

-(TB_TableView*)homeTableView
{
    if (!_homeTableView) {
        
        CGRect frame;

        if (kDevice_Is_iPhoneX) {
            
            frame=CGRectMake(0, 0, SCREEN_WIDTH, self.isSingeChoose==NO?SCREEN_HEIGHT-64-45*SCREEN_WIDTH/320.0-IPhoneXTableHeight:SCREEN_HEIGHT-64-IPhoneXTableHeight);
            
        }else{
            
            frame=CGRectMake(0, 0, SCREEN_WIDTH, self.isSingeChoose==NO?SCREEN_HEIGHT-64-45*SCREEN_WIDTH/320.0:SCREEN_HEIGHT-64);
        }
        
        _homeTableView=[[TB_TableView alloc]initWithFrame:frame style:UITableViewStylePlain];
        _homeTableView.delegate=self;
        
        _homeTableView.dataSource=self;
        
        _homeTableView.tableHeaderView=self.searchView;
        
        [_homeTableView registerNib:[UINib nibWithNibName:@"MsgStoreListTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        
        _homeTableView.tableFooterView=[[UIView alloc]init];
        
        [self configureNeedShowEmptyDataSetScrollView:_homeTableView];
        
        [self configScrollViewHeaderRefresh:_homeTableView autoRefresh:YES];
        
        [self configScrollViewFooterLoadMore:_homeTableView];

    }
    
    return _homeTableView;
    
}


/**
 *  MJ头部刷新(封装层方法, 需要子类继承或者重写)
 */
- (void)actionMJHeaderRefresh
{
    self.Reqmodel.currentPage=1;
    
    [self.homeTableView.mj_footer resetNoMoreData];
    
    [self loadDatas];
    
}

/**
 *  MJ底部加载(封装方法, 需要子类继承或者重写)
 */
- (void)actionMJFooterLoadMore
{

    self.Reqmodel.currentPage++;
    
    [self loadDatas];
}

#pragma mark-空白页点击事件
- (void)actionTapEmptyDataSetNodataView
{
    [self.homeTableView.mj_header beginRefreshing];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.dataArr.count;
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MsgStoreListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    if (self.isSingeChoose) {
        
            cell.SelectImage.hidden=YES;
        
    }

    switch (self.listType) {
        case 0:
        {
            ShopAndOpenData *data=self.dataArr[indexPath.row];
            
            cell.Sdata=data;
            
           //商户
        }
            break;
        case 1:
        {
           //检查员
            
            CheckManData*data=self.dataArr[indexPath.row];
            
            cell.Cdata=data;
            
        }
            break;
        case 2:
        {
            //开办方
            
            ShopAndOpenData *data=self.dataArr[indexPath.row];
            
            cell.Odata=data;
            
        }
            break;
        case 3:
        {
            //管理员
            
             CheckManData*data=self.dataArr[indexPath.row];
            
            cell.Mdata=data;
            
        }
            break;
            
        default:
            break;
    }
    
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewAutomaticDimension;
    
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.isSingeChoose) {
        
        switch (self.listType) {
            case 0:
            {
                ShopAndOpenData *data=self.dataArr[indexPath.row];
                
                _singleBlock(data.shopId,data.shopName);
                
                //商户
            }
                break;
           
            case 2:
            {
                //开办方
                
                ShopAndOpenData *data=self.dataArr[indexPath.row];
                
                _singleBlock(data.marketId,data.Name);
                
            }
                break;
           
                
            default:
                break;
        }
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
    
    switch (self.listType) {
        case 0:
        {
            ShopAndOpenData *data=self.dataArr[indexPath.row];
            
            data.Seleclt=!data.Seleclt;
            
            //商户
        }
            break;
        case 1:
        {
            //检查员
            
            CheckManData*data=self.dataArr[indexPath.row];
            
            data.Select=!data.Select;
            
        }
            break;
        case 2:
        {
            //开办方
            
            ShopAndOpenData *data=self.dataArr[indexPath.row];
            
            data.Seleclt=!data.Seleclt;
            
        }
            break;
        case 3:
        {
            //管理员
            
            CheckManData*data=self.dataArr[indexPath.row];
            
            data.Select=!data.Select;
            
        }
            break;
            
        default:
            break;
    }
    
    [self.homeTableView reloadData];
    
    }
}


-(void)GotoEdit:(UIButton*)sender
{
    sender.selected=!sender.selected;
    
    switch (self.listType) {
        case 0:
        {
      
            for (ShopAndOpenData *data in self.dataArr) {
                
                data.Seleclt=sender.selected;
                
            }
            
            //商户
        }
            break;
        case 1:
        {
            //检查员
            
            for (CheckManData*data in self.dataArr) {
                
                data.Select=sender.selected;
                
            }
    
            
        }
            break;
        case 2:
        {
            //开办方
            
            for (ShopAndOpenData *data in self.dataArr) {
                
                data.Seleclt=sender.selected;
                
            }
            
        }
            break;
        case 3:
        {
            //管理员
            
            for (CheckManData*data in self.dataArr) {
                
                data.Select=sender.selected;
                
            }
            
        }
            break;
            
        default:
            break;
    }
    
    [self.homeTableView reloadData];
    
}

-(void)loadDatas
{
    [self showLoadingAnimation];
    
    BaseReqApi *api=[[BaseReqApi alloc]initWithRequestUrl:self.DataReqStr andrequestTime:5 andParams:self.Reqmodel.mj_keyValues andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        [self stopLoadingAnimation];
     
        if (responseStatus==1) {
            //请求成功
            NSArray *responArr=responseObject[@"data"];
   
            if (responArr.count==0&&self.Reqmodel.currentPage==1) {

                [self.dataArr removeAllObjects];

                [self.homeTableView reloadData];

                self.Type=TBNODateType;

                self.btn.hidden=YES;
                
            }else if (responArr.count==0&&self.Reqmodel.currentPage!=1){

                [self.homeTableView.mj_footer endRefreshingWithNoMoreData];
                
                 self.btn.hidden=NO;

            } else{

                if (!self.isSingeChoose) {
                    
                      [self.view addSubview:self.AddBtn];
                    
                }
     
                self.btn.hidden=NO;
                
                if (self.Reqmodel.currentPage==1) {

                    [self.dataArr removeAllObjects];

                }

                switch (self.listType) {
                    case 0:
                    {
                        //商户
                        
                        self.StoreModel=[ShopAndOpenInforModel mj_objectWithKeyValues:responseObject];
                        
                        [self.dataArr addObjectsFromArray:self.StoreModel.data];
                        
                    }
                        break;
                    case 1:
                    {
                        //检查员
                        
                        self.CheckModel=[CheckManListModel mj_objectWithKeyValues:responseObject];
                        
                         [self.dataArr addObjectsFromArray:self.CheckModel.data];
                    }
                        break;
                    case 2:
                    {
                        //开办方
                        
                        self.OpenModel=[ShopAndOpenInforModel mj_objectWithKeyValues:responseObject];
                        
                         [self.dataArr addObjectsFromArray:self.OpenModel.data];
                        
                    }
                        break;
                    case 3:
                    {
                        //管理员
                        self.ManagerModel=[CheckManListModel mj_objectWithKeyValues:responseObject];
                        
                         [self.dataArr addObjectsFromArray:self.ManagerModel.data];
                    }
                        break;
                        
                    default:
                        break;
                }
                

                [self.homeTableView reloadData];

            }
            
            [self NetRequestSuccess:self scrollView:self.homeTableView];
            
        }else if (responseStatus==0||responseStatus==2)
        {
            if (self.Reqmodel.currentPage>1) {
                
                self.Reqmodel.currentPage=self.Reqmodel.currentPage-1;
            }
            
            [self NetRequestFail:self scrollView:self.homeTableView msg:message];
            
            self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
        }
        
    }];
    
    
    
}





-(UIButton*)AddBtn
{
    if (!_AddBtn) {
        
        _AddBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        
        if (kDevice_Is_iPhoneX) {
            
             _AddBtn.frame=CGRectMake(0, SCREEN_HEIGHT-45*SCREEN_WIDTH/320-64-IPhoneXTableHeight, SCREEN_WIDTH, 45*SCREEN_WIDTH/320);
            
        }else{
        
        _AddBtn.frame=CGRectMake(0, SCREEN_HEIGHT-45*SCREEN_WIDTH/320-64, SCREEN_WIDTH, 45*SCREEN_WIDTH/320);
            
        }
        [_AddBtn setTitle:@"确定" forState:UIControlStateNormal];
        
        [_AddBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        _AddBtn.titleLabel.font=[UIFont systemFontOfSize:21];
        
        _AddBtn.backgroundColor=TBColorProvideBtnbg;
        
        [self.view addSubview:_AddBtn];
        
        [_AddBtn addTarget:self action:@selector(addNewgoods) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _AddBtn;
}

#pragma mark-新增事件
-(void)addNewgoods
{
    NSMutableArray*PostArr=[NSMutableArray array];
    
    switch (self.listType) {
        case 0:
        {
            
            for (ShopAndOpenData *data in self.dataArr) {
                
                if (data.Seleclt) {
                    
                    NSMutableDictionary *postDic=[NSMutableDictionary dictionary];
                    
                    [postDic setValue:[NSString stringWithFormat:@"%ld",data.shopId] forKey:@"id"];
                    
                     [postDic setValue:data.shopName forKey:@"name"];
                    
                    [PostArr addObject:postDic];
                }
                
            }
            
            //商户
        }
            break;
        case 1:
        {
            //检查员
            
            for (CheckManData*data in self.dataArr) {
                
                if (data.Select) {
                    
                    NSMutableDictionary *postDic=[NSMutableDictionary dictionary];
                    
                    [postDic setValue:[NSString stringWithFormat:@"%ld",data.staffId] forKey:@"id"];
                    
                    [postDic setValue:data.staffName forKey:@"name"];
                    
                    [PostArr addObject:postDic];
                }
                
            }
            
            
        }
            break;
        case 2:
        {
            //开办方
            
            for (ShopAndOpenData *data in self.dataArr) {
                
                if (data.Seleclt) {
                    
                    NSMutableDictionary *postDic=[NSMutableDictionary dictionary];
                    
                    [postDic setValue:[NSString stringWithFormat:@"%ld",data.marketId] forKey:@"id"];
                    
                    [postDic setValue:data.Name forKey:@"name"];
                    
                    [PostArr addObject:postDic];
                }
                
            }
            
        }
            break;
        case 3:
        {
            //管理员
            
            for (CheckManData*data in self.dataArr) {
                
                if (data.Select) {
                    
                    NSMutableDictionary *postDic=[NSMutableDictionary dictionary];
                    
                    [postDic setValue:[NSString stringWithFormat:@"%ld",data.staffId] forKey:@"id"];
                    
                    [postDic setValue:data.staffName forKey:@"name"];
                    
                    [PostArr addObject:postDic];
                }
                
            }
            
        }
            break;
            
        default:
            break;
    }
    
    
    NSString*postStr=[HelpWay arrToJSON:PostArr];
    
    _block(postStr,PostArr.count);
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
